/**
 * 数据库工具类
 * 方法:
 * 	query 查询
 */
var _ljs=require("../mmlog").root("[sever/db/db.js]");

var _config=require("../config");

var mysql = require("mysql");

/*var dataSource = {
	multipleStatements: true,
	connectionLimit: 20,
	host: '121.42.147.235',
	user: 'root',
	password: 'abc123',
	database: 'mmlink'
}*/

var pool = mysql.createPool(_config.db());



var _self = {

	test:function(){
		
		return "hello"
		
	},
	/**
	 * 
	 * @param {Object} param
	 * @param {Object} callback
	 */
	query: function(param, callback) {
		pool.getConnection(function(error, conn) {
			if (error) {
				_ljs.error("数据库连接失败:\n", error);
				callback(null);
				return
			}

			var p = null;

			if (typeof param === "string") {
				p = param;
			} else {
				p = {
					sql: param.sql,
					values: param.values,
					timeout: 4000
				}
			}

			conn.query(p, function(error, result, fields) {
				_ljs.info("数据库操作:\n", param.sql || p);
				if (error) {
					_ljs.error("数据库操作失败:\n", error);
					callback && callback(-1);
				} else {
					_ljs.info("数据库操作成功:\n");
					callback && callback(result);
				}
				conn.release();
			})
		})
	}

}

module.exports = _self;